Coding for DS and DM
R coding module

Lecture 10

Andrea Cappozzo
andrea.cappozzo@unimi.it
AndreaCappozzo
andreacappozzo.rbind.io

Exam simulation

30:00

Question 1

Given the following matrix:

mat <- matrix(1:9, nrow = 3, byrow=FALSE)

What does mat[2, 3] return?

mat[2, 3]
[1] 8

Question 2

Consider the following R code to create a list called my_list:

my_list <- list(nums = 1:3, letters = c("A", "B", "C"), flag = TRUE)

Which of the following methods can be used to extract the character "B" from my_list?

Question 2

Consider the following R code to create a list called my_list:

my_list <- list(nums = 1:3, letters = c("A", "B", "C"), flag = TRUE)

Which of the following methods can be used to extract the character "B" from my_list?

Question 3

Consider the following R code:

x <- 1
y <- 0
while (x <= 5) {
  y <- y + x
  x <- x + 1
}

What is the value of y after the while loop completes?

y
[1] 15

Question 4

What is the correct way to define a user-defined function in R that calculates the square of a number?

Question 4

What is the correct way to define a user-defined function in R that calculates the square of a number?

Question 5

You want to create a customized scatter plot in Base R where:
- The points are colored based on a categorical variable group.
- The axes are labeled as “X-Axis Label” and “Y-Axis Label”.

Which of the following code snippets accomplishes this?

Question 5

x <- y <- 1:10
group <- letters[1:10]
plot(x, y, col = as.factor(group), pch = 19,
     xlab = "X-Axis Label", ylab = "Y-Axis Label")

Question 6:

Which of the following is the first step when creating a new R package using devtools?

Question 6:

Which of the following is the first step when creating a new R package using devtools?

Question 7

What is the output of the following code?

library(magrittr)
df <- data.frame(x = rnorm(100), y = rnorm(100), z = rnorm(100))
res <- df %>% list(y = nrow(.), z = ncol(.))
tibble::glimpse(res)
List of 3
 $  :'data.frame':  100 obs. of  3 variables:
  ..$ x: num [1:100] -0.561 1.429 0.277 0.459 -0.151 ...
  ..$ y: num [1:100] -0.535 -1.363 0.656 0.316 0.89 ...
  ..$ z: num [1:100] 0.759 -0.906 0.451 1.074 -0.394 ...
 $ y: int 100
 $ z: int 3

Question 8:

Select all the correct answers.

---
title: "Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    css: css/styles-default.css
    logo: img/logo.png
runtime: shiny
---

Question 8:

Select all the correct answers.

---
title: "Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    css: css/styles-default.css
    logo: img/logo.png
runtime: shiny
---